<!-- 批量手动分配 -->
<template>
  <glsx-dialog :show="value" title="批量手动分配" @close="closeDialog()">
    <div class="">
      <div class="title">已选中{{staticInfo.assignClueCnt}}条线索，其中主诉{{staticInfo.mainProjectClueCnt}}条，非主诉{{staticInfo.otherProjectClueCnt}}条</div>
      <div class="sub_title">当前可分配用户及规则(分配前请确认规则是否有误)：</div>
      <el-form ref="form" :model="formInline" :rules="formRules" size="small">
        <el-form-item label="分配人：" prop="assignmentUserIds">
          <el-select v-model="formInline.assignmentUserIds" filterable>
            <el-option v-for="(item,index) in userList" :key="index" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <div slot="footer">
      <el-button type="primary" size="small" :loading="loading" @click="saveInfo">确认分配</el-button>
      <el-button type="default" size="small" @click="closeDialog()">取消</el-button>
    </div>
  </glsx-dialog>
</template>
<script>
import {
  GET_CLUE_ASSIGNCNT,
  GET_ASSIGNMENT_OPTIONS,
  SAVE_CLUE_MANUALASSIGN,
} from "@/http/clues";
import { deepClone, showModal, showToast } from "@/common/util";
export default {
  name: "HandAssign",
  props: {
    value: {
      type: Boolean,
      default: false,
    },
    list: {
      type: Array,
      default: () => {
        return [];
      },
    },
  },
  data() {
    return {
      formInline: {},
      staticInfo: {},
      userList: [],
      formRules: {
        assignmentUserIds: [
          { required: true, message: "请选择分配人", trigger: "blue" },
        ],
      },
      loading: false,
    };
  },
  created() {
    this.getInfo();
    this.getList();
  },
  mounted() {},
  methods: {
    // 关闭弹框
    closeDialog() {
      this.$emit("input", false);
    },

    // 获取数量
    getInfo() {
      let _this = this;
      let list = this.list || [];
      let arr = [];
      list.map((k) => {
        arr.push(k.id);
      });
      GET_CLUE_ASSIGNCNT({ clueIds: arr }).then((res) => {
        _this.staticInfo = res.data || {};
      });
    },

    // 获取可分配人下拉列表
    getList() {
      GET_ASSIGNMENT_OPTIONS().then((res) => {
        console.log(res);
        this.userList = res.data || [];
      });
    },

    // 保存数据
    saveInfo() {
      let _this = this;
      _this.$refs.form.validate((valid) => {
        if (!valid) {
          return false;
        }
        let oParams = deepClone(_this.formInline);
        let list = this.list || [];
        let arr = [];
        list.map((k) => {
          arr.push(k.id);
        });
        oParams.clueIds = arr;
        oParams.assignmentUserIds = [oParams.assignmentUserIds];
        _this.loading = true;
        SAVE_CLUE_MANUALASSIGN(oParams)
          .then((res) => {
            _this.loading = false;
            let obj = res.data || {};
            if (res.code != 200) {
              return false;
            }

            _this.closeDialog();
            _this.$emit("change");

            if (obj.tips) {
              showModal({
                message: obj.tips,
                showCancelButton: true,
                type: "warning",
              });
              return false;
            }

            showToast({
              message: "分配成功",
              type: "success",
            });
          })
          .catch(() => {
            _this.loading = false;
          });
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.sub_title {
  margin: 10px 0;
}
.cell {
  display: flex;
  align-items: center;
  margin: 10px 0;
}
</style>